
$(document).ready(function(){
    var dates = {
        toMinDate: null,
        minDate: null,
        maxDate: null
    }
    var collections = {
        events: new Array(),
        countries: new Array(),
        cities: new Array()
    }
    var controls = {
        panel: $('#filter-event'),
        text: $('#text'),
        datepicker: $('#from, #to'),
        city: $('#cities'),
        country: $('#countries')
    }
    var template = new Template();
    var map = new Map($('#map').get(0));
    var filter = new Filter(map, template, collections, controls);
    var event = new Event(map, template, collections, controls, dates);
    
    // load templates and events async
    $.get($('#event_tmp').val(), {
        format: 'html'
    }, function(tmp){
        template.setEventTemplate(tmp);
        if($('#artist_event_json').length)
            event.artist.getEvents($('#artist_event_json').val());
        else if($('#location_event_json').length){
//            var location = new Location();
//            location.search($('#location_city').val());
            event.location.getEvents($('#location_event_json').val());
        } else if($('#venue_event_json').length) {
            filter.setVenue(true);
            event.venue.getEvents($('#venue_event_json').val());
        } else
            event.location.getGeoLocation();

    });

    // event click
    $("#events li div.event").live('click', function() {
        map.window.toggle($(this).children("input[type=hidden]:first").val());
    });

    // event mouseenter
    $("li div.event").live('mouseenter', function(){
        map.window.open($(this).children("input[type=hidden]:first").val());
    });

    // event mouseleave
    $("li div.event").live('mouseleave', function(){
        map.window.close($(this).children("input[type=hidden]:first").val());
    });

    // filter any text
    $('#text').keyup(function() {
        filter.text($('#text').val());
    });

    // filter by country
    $('#countries').change(function() {
        var country = $('#countries :selected').text();
        var city = $('#cities :selected').text();
        filter.city.fill(country);
        filter.location(country, city, country)
    });
    
    // filter by city
    $('#cities').change(function() {
        var country = $('#countries :selected').text();
        var city = $('#cities :selected').text();
        filter.location(city, city, country)
    });

    // datepicker
    $( "#from, #to" ).datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 1,
        beforeShow: function(input, inst) {
            $('#from, #to').datepicker( "option", "minDate", (dates.toMinDate > dates.minDate && this.id == "to") ? dates.toMinDate : dates.minDate );
            $('#from, #to').datepicker( "option", "maxDate", dates.maxDate );
        },
        onSelect: function( selectedDate ) {
            var instance = $( this ).data( "datepicker" ),
            date = $.datepicker.parseDate(
                instance.settings.dateFormat ||
                $.datepicker._defaults.dateFormat,
                selectedDate, instance.settings );
            if(this.id == "from") {
                dates.toMinDate = date;
                filter.date(date, $( "#from, #to" ).not(this).datepicker("getDate"));
            }
            else
            filter.date($( "#from, #to" ).not(this).datepicker("getDate"), date);
            }
            });
        });